//
//  CourseLeaderViewController.m
//  ShowYork
//
//  Created by hxh on 2020/10/22.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonLeaderViewController.h"
#import "LessonLeaderCell.h"
#import "LessonLeaderModel.h"
@interface LessonLeaderViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)LessonLeaderModel *dtlModel;
@property (nonatomic,strong)UIView *navView;
@property (nonatomic,strong)UIImageView *backImgView;
@property (nonatomic,strong)UIButton *backBtn;
@end

@implementation LessonLeaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHideNavBar = YES;
    
    [self setUI];
    [self loadDataSource];
    
    // Do any additional setup after loading the view.
}
- (void)setUI{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.estimatedRowHeight = 100;
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    

    self.tableView.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    if (@available(iOS 11.0,*)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.navView = [[UIView alloc]init];
    self.navView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(kNavBarHeight);
    }];
    
    self.backImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_back_normal_tag"]];
    [self.navView addSubview:self.backImgView];
    [self.backImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.navView).offset(-8);
        make.left.equalTo(self.navView).offset(16);
    }];

    self.backBtn = [[UIButton alloc]init];
    [self.backBtn addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.navView addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.navView);
        make.right.equalTo(self.backImgView.mas_right).offset(10);
        make.top.equalTo(self.backImgView.mas_top).offset(-10);
    }];
}
- (void)backMethod{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadDataSource{
    
    [self showDataLoadState];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"lessonId"] = self.lessonId;
    param[@"md5"] = [RPAppManager md5StrWithApi:GetTeacherLeader];
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(GetTeacherLeader) parames:param success:^(id responseObj) {
        [self hideDataLoadLoadState];
        self.dtlModel = [LessonLeaderModel mj_objectWithKeyValues:responseObj[RESPONSE]];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self hideDataLoadLoadState];
        [WHToast showMessage:error.userInfo[@"info"] duration:2 finishHandler:nil];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.dtlModel == nil) {
        return 0;
    }
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LessonLeaderCell *cell = [LessonLeaderCell cellWithTableView:tableView];
    [cell setModel:self.dtlModel];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y <= 0) {
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
}

@end
